<?php
header("Content-type:text/html; charset=utf-8");
require_once ('include/conn.php');
require_once ('system.class.php');
function arclist($args, $content, &$smarty){
	$num=$args['num'];
	$channel_id_info=$args['channel_id'];
	$channel_id=explode(",", $channel_id_info);
	$channel_num=count($channel_id);
	if($num==""){$num=8;}
	$time=time();
	$limit_num=$num-1;
	/*用于计数*/
	$article_num=0;
	/*得到栏目名称*/
	for($i=0;$i<$channel_num;$i++){
		$channel_infor=getChannelInfor($channel_id[$i]);
		$article_main_channel=$channel_id[$i];
		$channel_name[$article_main_channel]=$channel_infor["channel_name"];
	}
	/*更新置顶的文章,将过期的文件取消置顶设置*/
	$sql_update_is_top_articles="update article_normal set article_is_top=0 where article_is_top=1 and article_top_time<$time;";
	$result_update_is_top_articles=mysql_query($sql_update_is_top_articles);
	/*获取置顶文件*/
	$sql_get_is_top_articles="select article_id,article_title,article_is_top,article_publish_time,article_modify_time,article_is_extends_url,article_extends_url,article_main_channel from article_normal where article_status=1 and article_is_top=1 and article_main_channel in($channel_id_info) order by article_modify_time desc limit 0,$limit_num;";
	$result_get_is_top_articles=mysql_query($sql_get_is_top_articles);
	$num_is_top_articles=mysql_num_rows($result_get_is_top_articles);
	if($num_is_top_articles>0){
		while($row_get_is_top_articles=mysql_fetch_row($result_get_is_top_articles)){
			$article_id=$row_get_is_top_articles[0];
			$article_main_channel=$row_get_is_top_articles[7];
			$arr[$article_id]['channel_name']=$channel_name[$article_main_channel];
			$arr[$article_id]['id']=$row_get_is_top_articles[0];
			$arr[$article_id]['title']=$row_get_is_top_articles[1];
			if($row_get_is_top_articles[5]==1){
				$arr[$article_id]['url']=$row_get_is_top_articles[6];
			}else{
				$arr[$article_id]['channel_url']=CIE."/channel.php?channel_id=$article_main_channel";
				$arr[$article_id]['url']=CIE."/article.php?article_id=$row_get_is_top_articles[0]&channel_id=$article_main_channel";	
			}
			$arr[$article_id]['publish_time']=date('Y-m-d',$row_get_is_top_articles[3]);
			$arr[$article_id]['top']=($row_get_is_top_articles[2]==1)?'true':'false';
			$now_time=time();
			$arr[$article_id]['new']=($now_time-$row_get_is_top_articles[3]<=60*60*24)?'true':'false';
			$article_num++;
			if($article_num==$num){break;}
		}
	}
	/*如果置顶文件个数不够，选最新更新的文章*/
	if($article_num<$num){
		$new_limit=$num-$article_num;
	$sql_get_new_articles="select article_id,article_title,article_publish_time,article_modify_time,article_is_extends_url,article_extends_url,article_main_channel,article_is_top from article_normal where article_status=1 and article_is_top=0 and article_main_channel in ($channel_id_info) order by article_modify_time desc limit 0,$new_limit;";
	$result_get_new_articles=mysql_query($sql_get_new_articles);
	$num_get_new_articles=mysql_num_rows($result_get_new_articles);
	if($num_get_new_articles>0){
	while($row_get_new_articles=mysql_fetch_row($result_get_new_articles)){
		$article_id=$row_get_new_articles[0];
		$article_main_channel=$row_get_new_articles[6];
		@$arr[$article_id]['channel_name']=$channel_name[$article_main_channel];
		$arr[$article_id]['id']=$row_get_new_articles[0];
		$arr[$article_id]['title']=$row_get_new_articles[1];
		$arr[$article_id]['publish_time']=date('Y-m-d',$row_get_new_articles[2]);
		if($row_get_new_articles[4]==1){
			$arr[$article_id]['url']=$row_get_new_articles[5];
		}
		else{
			$arr[$article_id]['channel_url']=CIE."/channel.php?channel_id=$article_main_channel";
			$arr[$article_id]['url']=CIE."/article.php?article_id=$row_get_new_articles[0]&channel_id=$article_main_channel";
		}
		$arr[$article_id]['top']=($row_get_new_articles[7]==1)?'true':'false';
		$now_time=time();
		$arr[$article_id]['new']=($now_time-$row_get_new_articles[2]<=60*60*24)?'true':'false';
		$article_num++;
		if($article_num==$num){break;}
	}
	}
	}
	if(empty($arr)){
		$arr=null;
	}
	$smarty->assign("list",$arr);
	return $content;
}
function getChannelInfor($channel_id){
	$sql_get_channel_infor="select channel_visit_path,channel_name from channel where channel_id=$channel_id";
	$result_get_channel_infor=mysql_query($sql_get_channel_infor);
	$row_get_channel_infor=mysql_fetch_row($result_get_channel_infor);
	return array("channel_visit_path"=>$row_get_channel_infor[0],"channel_name"=>$row_get_channel_infor[1]);

}